//
//  SalesReturnVC.m
//  JinFuThree
//
//  Created by 毛韶谦 on 2018/9/12.
//  Copyright © 2018年 毛韶谦. All rights reserved.
//

#import "SalesReturnVC.h"
#import "ImportTextView.h"
#import "SalesReturnHeaderView.h"

@interface SalesReturnVC ()<UITextViewDelegate>

@property (nonatomic, strong) UIScrollView *mainScrollView;
@property (nonatomic, strong) SalesReturnHeaderView *goodsView;
@property (nonatomic, strong) ImportTextView *orderCodeView;
@property (nonatomic, strong) ImportTextView *orderTimeView;
@property (nonatomic, strong) ImportTextView *payWayView;

@property (nonatomic, strong) UIView *bottomView;
@property(nonatomic,strong)UILabel *beiZhuCountLabel;

@property (nonatomic, strong) UIButton *nextBtn;

@end

@implementation SalesReturnVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self updateInfo];
}

- (void)initUI {
    
    self.title = @"退货申请";
    
    UILabel *goodsLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, ScreenWidthMao - 40, 35)];
    goodsLabel.text = @"商品详情";
    goodsLabel.textColor = HexColor(0x666666);
    goodsLabel.font = [UIFont systemFontOfSize:13];
    [self.mainScrollView addSubview:goodsLabel];
    
    self.goodsView.frame = CGRectMake(0, CGRectGetMaxY(goodsLabel.frame), ScreenWidthMao, 140);
    
    UILabel *orderLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(self.goodsView.frame), ScreenWidthMao - 40, 35)];
    orderLabel.text = @"订单详情";
    orderLabel.textColor = HexColor(0x666666);
    orderLabel.font = [UIFont systemFontOfSize:13];
    [self.mainScrollView addSubview:orderLabel];
    
    self.orderCodeView.frame = CGRectMake(0, CGRectGetMaxY(orderLabel.frame), ScreenWidthMao, 55);
    self.orderTimeView.frame = CGRectMake(0, CGRectGetMaxY(self.orderCodeView.frame), ScreenWidthMao, 55);
    self.payWayView.frame = CGRectMake(0, CGRectGetMaxY(self.orderTimeView.frame), ScreenWidthMao, 55);

    self.bottomView.frame = CGRectMake(0, CGRectGetMaxY(self.payWayView.frame) + 10, ScreenWidthMao, 124);
    
    [self.beiZhuCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.bottomView.mas_right).offset(-20);
        make.bottom.mas_equalTo(self.bottomView).offset(-5);
    }];
    
    [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view);
        make.height.mas_equalTo(50);
    }];
}

- (void)updateInfo {
    
    self.orderCodeView.leftTextView.text = @"1205 1713 9880 1121 6013";
    self.orderTimeView.leftTextView.text = @"2018-04-08";
    self.payWayView.leftTextView.text = @"在线支付";
    
    self.goodsView.leftImageView.backgroundColor = [UIColor redColor];
    self.goodsView.titleLabel.text = @"OPPO R15全面屏双摄拍照 6G+128G 星空紫 全网通 移动联通电信4G 双卡…";
    self.goodsView.numberLabel.text = @"x1";
    self.goodsView.priceLabel.text = @"￥7599.00";
    
    
}

- (void)backAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)tiJiaoAction {
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"确 定" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [ShowInfoBtnView ShowInfoView:self.view withImage:[UIImage imageNamed:@"Return Request"] WithTitle:@"退货申请提交成功" WithInfo:@"退货将在1～7个工作日内退还" WithBtn:btn];
}

#pragma mark -----TextViewDelegate-----

- (void)textViewDidChange:(UITextView *)textView {
    self.beiZhuCountLabel.text = [NSString stringWithFormat:@"%lu/500",(unsigned long)textView.text.length];
    self.beiZhuCountLabel.accessibilityLabel = textView.text;
    if (textView.text.length > 500) {
        self.beiZhuCountLabel.textColor = [UIColor redColor];
    }else {
        self.beiZhuCountLabel.textColor = HexColor(0xaaaaaa);
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (textView.text.length < 500) {
        return YES;
    }
    if (text.length > 0) {
        return NO;
    }
    return YES;
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
    if ([textView.text isEqualToString:@"请说明退货原因"]) {
        textView.text = @"";
        textView.textColor = HexColor(0x666666);
    }
}
- (void)textViewDidEndEditing:(UITextView *)textView {
    if ([textView.text isEqualToString:@""]) {
        textView.text = @"请说明退货原因";
        textView.textColor = HexColor(0xaaaaaa);
        self.beiZhuCountLabel.accessibilityLabel = @"";
    }else {
        self.beiZhuCountLabel.accessibilityLabel = textView.text;
    }
}

- (UIScrollView *)mainScrollView {
    
    if (!_mainScrollView) {
        _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao, ScreenHeight - SafeAreaTopHeight)];
        _mainScrollView.contentSize = CGSizeMake(ScreenWidthMao, 600);
        _mainScrollView.showsVerticalScrollIndicator = NO;
        _mainScrollView.showsHorizontalScrollIndicator = NO;
        _mainScrollView.bounces = NO;
        _mainScrollView.backgroundColor = ListMaoBackgroundColor;
        
        [self.view addSubview:_mainScrollView];
    }
    return _mainScrollView;
}

- (SalesReturnHeaderView *)goodsView {
    
    if (!_goodsView) {
        _goodsView = [[SalesReturnHeaderView alloc] init];
        
        [self.mainScrollView addSubview:_goodsView];
    }
    return _goodsView;
}
- (ImportTextView *)orderCodeView {
    
    if (!_orderCodeView) {
        _orderCodeView = [[ImportTextView alloc] init];
        _orderCodeView.userInteractionEnabled = NO;
        _orderCodeView.leftTextView.textColor = HexColor(0x666666);
        _orderCodeView.titleLabel.text = @"订单编号";
        
        [self.mainScrollView addSubview:_orderCodeView];
    }
    return _orderCodeView;
}
- (ImportTextView *)orderTimeView {
    
    if (!_orderTimeView) {
        _orderTimeView = [[ImportTextView alloc] init];
        _orderTimeView.userInteractionEnabled = NO;
        _orderTimeView.leftTextView.textColor = HexColor(0x666666);
        _orderTimeView.titleLabel.text = @"下单时间";
        
        [self.mainScrollView addSubview:_orderTimeView];
    }
    return _orderTimeView;
}
- (ImportTextView *)payWayView {
    
    if (!_payWayView) {
        _payWayView = [[ImportTextView alloc] init];
        _payWayView.userInteractionEnabled = NO;
        _payWayView.leftTextView.textColor = HexColor(0x666666);
        _payWayView.titleLabel.text = @"支付方式";
        _payWayView.lineImageView.hidden = YES;
        
        [self.mainScrollView addSubview:_payWayView];
    }
    return _payWayView;
}

- (UIView *)bottomView {
    
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
        _bottomView.backgroundColor = [UIColor whiteColor];
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 15, 65, 21)];
        titleLabel.text = @"退货原因";
        titleLabel.textColor = HexColor(0x333333);
        titleLabel.font = [UIFont systemFontOfSize:15];
        [_bottomView addSubview:titleLabel];
        
        UILabel *tinkLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(titleLabel.frame), 15, 120, 21)];
        tinkLabel.text = @"(至少说明一个原因)";
        tinkLabel.textColor = HexColor(0xaaaaaa);
        tinkLabel.font = [UIFont systemFontOfSize:13];
        [_bottomView addSubview:tinkLabel];
        
        UITextView *beiZhuTextView = [[UITextView alloc] initWithFrame:CGRectMake(15, 40, ScreenWidthMao - 30, 70)];
        beiZhuTextView.text = @"请说明退货原因";
        beiZhuTextView.font = [UIFont systemFontOfSize:13];
        beiZhuTextView.textColor = HexColor(0xaaaaaa);
        beiZhuTextView.delegate = self;
        [_bottomView addSubview:beiZhuTextView];
        
        [self.mainScrollView addSubview:_bottomView];
    }
    return _bottomView;
}
- (UILabel *)beiZhuCountLabel {
    if (!_beiZhuCountLabel) {
        _beiZhuCountLabel = [[UILabel alloc] init];
        _beiZhuCountLabel.font = [UIFont systemFontOfSize:13];
        _beiZhuCountLabel.textColor = HexColor(0x666666);
        _beiZhuCountLabel.text = @"0/500";
        _beiZhuCountLabel.accessibilityLabel = @"";
        [self.bottomView addSubview:_beiZhuCountLabel];
    }
    return _beiZhuCountLabel;
}
- (UIButton *)nextBtn {
    
    if (!_nextBtn) {
        _nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.colors = @[(__bridge id)HexColor(0xD6C096).CGColor, (__bridge id)HexColor(0xBEA471).CGColor];
        gradientLayer.locations = @[@0, @1];
        gradientLayer.startPoint = CGPointMake(0, 1);
        gradientLayer.endPoint = CGPointMake(1, 1);
        gradientLayer.frame = CGRectMake(0, 0, ScreenWidthMao, 50);
        gradientLayer.cornerRadius = 4;
        gradientLayer.masksToBounds = YES;
        [_nextBtn.layer addSublayer:gradientLayer];
        [_nextBtn addTarget:self action:@selector(tiJiaoAction) forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *label = [[UILabel alloc] init];
        label.font = [UIFont systemFontOfSize:15];
        label.textColor = [UIColor whiteColor];
        label.text = @"提 交";
        [_nextBtn addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(_nextBtn);
        }];
        
        [self.view addSubview:_nextBtn];
    }
    return _nextBtn;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
